package com.luck.easyfoodbatch.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.luck.easyfoodbatch.App;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.dialog.CustomDialog;
import com.luck.easyfoodbatch.simcpux.Constants;
import com.luck.easyfoodbatch.util.SharedPreferencesUtil;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            int code = resp.errCode;
            String order_id = "";
            switch (code) {
                case 0:
                    showApply("支付成功！", code);
                    break;
                case -1:
                    showApply("支付失败，请检查签名是否正确！", code);
                    SharedPreferencesUtil.saveData(this, "order_id", "");
                    break;
                case -2:
                    showApply("取消支付！", code);
                    SharedPreferencesUtil.saveData(this, "order_id", "");
                    break;
            }
        }
    }

    /**
     * 支付结果
     */
    private void showApply(String content, final int code) {
        final CustomDialog dialog = new CustomDialog(this, App.screenWidth * 3 / 4,
                App.screenHeight / 4, R.layout.wind_clock_dialog_xml, R.style.Theme_dialog);
        TextView tv_title = (TextView) dialog.findViewById(R.id.tv_title);
        Button btn_commit = (Button) dialog.findViewById(R.id.btn_commit);
        TextView tv_content = (TextView) dialog.findViewById(R.id.tv_content);
        dialog.setCancelable(false);
        tv_title.setText("支付结果");
        tv_content.setText(content);
        btn_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (code == 0) {
                    // 支付成功，才刷新订单列表
                    sendBroadcast("action.finishApplyActivity", "action.app.refresh.evaluate", "action.app.integral");
                }
                finish();
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return false;
        }
        return false;
    }


    /**
     * 广播发送者
     *
     * @param action
     */
    protected void sendBroadcast(String... action) {
        Intent intent = new Intent();
        for (int i = 0; i < action.length; i++) {
            intent.setAction(action[i]);
            sendBroadcast(intent);
        }
    }
}